﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PH.Model.Accounts;
namespace PH.Data.Accounts
{
    public class RoleCommand
    {
        public static void SaveCheck(RoleModel role, DBCentre db)
        {
            if (string.IsNullOrEmpty(role.RoleName))
                throw new Utils.PhException("请提供角色名称");

            
        }

        public static RoleModel CreateNewRole(RoleModel role)
        {
            if (role != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(role, db);
                    var dRole = Convertor.ConvertFrom<Role, RoleModel>(role);
                    db.DataContext.Roles.Add(dRole);
                    db.DataContext.SaveChanges();
                    role.ID = dRole.ID;
                    return role;
                }
            }
            return null;
        }

        public static void UpdateRole(RoleModel role)
        {
            if (role != null && role.ID > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dRole = db.DataContext.Roles.FirstOrDefault(p => p.ID == role.ID);
                    if (dRole != null)
                    {
                        SaveCheck(role, db);
                        Convertor.UpdateValue(role, dRole);
                        db.DataContext.SaveChanges();
                    }
                    else
                        throw new Utils.PhException("不存在的角色");
                }
            }
        }

        public static RoleModel GetRole(int roleID)
        {
            if (roleID>0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dRole = db.DataContext.Roles.FirstOrDefault(p => p.ID == roleID);
                    if (dRole != null)
                    {
                        var role = Convertor.ConvertTo<RoleModel, Role>(dRole);
                        return role;
                    }
                }
            }
            return null;
        }
        public static RoleModel GetRole(string rolename)
        {
            if (!string.IsNullOrEmpty(rolename))
            {
                using (DBCentre db = new DBCentre())
                {
                    var dRole = db.DataContext.Roles.FirstOrDefault(p => p.RoleName == rolename);
                    if (dRole != null)
                    {
                        var role = Convertor.ConvertTo<RoleModel, Role>(dRole);
                        return role;
                    }
                }
            }
            return null;
        }

        public static RoleCollection GetAllRoles()
        {
            RoleCollection results = new RoleCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Role> q = db.DataContext.Roles;
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    var lst = q.ToList();
                    results = Convertor.ConvertCollection<RoleModel, Role, RoleCollection>(lst);
                    results.TotalRecordsCount = n;
                }
            }
            return results;
        }

        public static RoleCollection GetRolesForUser(string userName)
        {
            var results = new RoleCollection();
            using (DBCentre db = new DBCentre())
            {
                var guid = new Guid(userName);
                IQueryable<Role> q = db.DataContext.Roles;
                q = q.Where(p => p.Users.Any(o => o.Guid == guid));
                var n = q.Count();
                if (n > 0)
                {
                    var lst = q.ToList();
                    results = Convertor.ConvertCollection<RoleModel, Role, RoleCollection>(lst);
                    results.TotalRecordsCount = n;
                }
            }
            return results;
        }

        public static bool RoleExists(string roleName)
        {
            if (!string.IsNullOrEmpty(roleName))
            {
                using (DBCentre db = new DBCentre())
                {
                    return db.DataContext.Roles.Any(p => p.RoleName == roleName);
                }
            }
            return false;
        }
    }
}
